# Description:
#   Summary of regular expression matching functions based on re module
# Author:
#   Xiaofeng Du
# E-mail:
#   791419282@qq.com
# Date YYYY-MM-DD:
#   2022-03-04
# Reference:
#   https://www.runoob.com/python/python-reg-expressions.html
#   https://docs.python.org/zh-cn/3/library/re.html

import re
from bs4 import BeautifulSoup


# 判断字符串是否为浮点
def isfloat(s)->bool:
    mp = ['1','2','3','4','5','6','7','8','9','0','.']
    for i in s:
        if i not in mp:
            return False
    return True if type(eval(s)) == float else False

# 删除大写字母
def subLowwer(a):
    return re.sub(r'[A-Z]', '', a)


# 寻找汉字
def find_chinese(string):

    pattern = re.compile(r'[^\u4e00-\u9fa5]（）')

    chinese = re.sub(pattern, '', string)

    return(chinese)

# 删除汉字
def find_unchinese(string): 

    pattern = re.compile(r'[\u4e00-\u9fa5]')

    unchinese = re.sub(pattern,'',string)
    
    return(unchinese)

# 删除中文符号
def find_unchinesesin(string, temp = r'[（），。、]'):
    
    pattern = re.compile(temp)

    unchinese = re.sub(pattern,'',string)

    return unchinese

# 替换换行符为空格
def emplace_enter(string):
    
    pattern = re.compile(r'[\n]')

    unchinese = re.sub(pattern,' ',string)

    return unchinese

# 根据正则表达式来寻找字符串对象
def find_target(string,PATTERN,unchinese=False,placeholder="-"): 

    pattern = re.compile(PATTERN)
    m = pattern.search(string)
    if not m:
        return placeholder  # 没有成功匹配 范围 placeholder
    if m.lastindex >= 1:
        return m.group(1) if not unchinese else find_unchinese(m.group(1))

# 专门为了删除ali云的使用场景中多出来的尾巴而设计的函数
# "汉字汉字汉字汉字汉字    g7"
def cutTails(string):
    l = len(string)
    for i in range(1,l+1):
        if string[l-i] == ' ':
            return string[:l-i].strip()
    return string

# 根据正则表达式来寻找字符串对象
def match_target(string,PATTERN,unchinese=False,placeholder="-",isList=False): 

    pattern = re.compile(PATTERN)
    m = pattern.search(string)
    
    if not m:
        return placeholder  # 没有成功匹配 范围 noneVal
    elif not isList:
        return m.group(0)

# 根据正则表达式来删除对象
def del_target(string,PATTERN,unchinese=False,placeholder="-"): 

    pattern = re.compile(PATTERN)

    unchinese = re.sub(pattern,' ',string)

    return(unchinese)

# 寻找时钟频率
def fing_ClockSpeed(string):
    temp = re.findall(r'-?\d+\.?\d*e?-?\d*?', string)
    res = ''
    vector = []
    for i in temp:
        if isfloat(i):
            vector.append(i)

    if len(vector) == 0:
        return '-/- GHz'
    for i in vector:
        if res:
            res += '/'
        res += i

    if len(vector) == 1:
        res += '/-'
    if res:
        res += 'GHz'
    
    return res

# 寻找CPU号码整数（长度4-10）
def find_CPUnum(string):
    temp = re.findall(r'\d{4,10}', string)
    print("number in CPU item: ", temp)
    if temp:
        return temp[0]
    return ' '

# 寻找整数
def find_num(string):
    temp = re.findall(r'\d+', string)
    # print("number in CPU item: ", temp)
    if temp:
        return temp[0]
    return ' '

# 翻译 details 表格 中的一些中文字符
def find_details_translator(InputStr):
    InputStr = str(InputStr)
    InputStr = InputStr.replace('最高',' MAX ')
    InputStr =InputStr.replace('无','-')
    InputStr = InputStr.replace('万','0k')
    InputStr = InputStr.replace('本地',' local ')
    InputStr = InputStr.replace('硬盘','disk')
    InputStr = InputStr.replace('搭载','')
    InputStr = InputStr.replace('块',' pieces ')
    InputStr = InputStr.replace('是',' True ')
    return InputStr

# 基于BeautifulSoup的爬虫寻找目标段落
def BS_find_target(html,tar):
    soup=BeautifulSoup(html,'html.parser')
    # 自动补齐html文本中成对的标签
    soup.prettify()
    return soup.find_all(text=re.compile(r'.*?'+tar+'.*?'))[0]
